package com.back.Controller;

import cn.hutool.core.util.StrUtil;
import com.back.Dao.R;
import com.back.Dao.User;
import com.back.Service.UserService;
import com.back.common.AuthAccess;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Resource
    UserService userService;
    @AuthAccess
    @GetMapping("/")
    public R hello(){ return  R.success("success");}
    @PostMapping("/userLogin")
    public R login(@RequestBody User user){
        if(StrUtil.isBlank(user.getUserName()) || StrUtil.isBlank(user.getUserPwd())){
            return R.error("数据输入不合法");
        }
        user = userService.login(user);
        return R.success(user);
    }
    @AuthAccess
    @PostMapping("/register")
    public R register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUserName()) || StrUtil.isBlank(user.getUserPwd())) {
            System.out.print(user.getUserName());
            return R.error("数据输入不合法");
        }
        if (user.getUserName().length() > 10 || user.getUserPwd().length() > 20) {
            return R.error("数据输入不合法");
        }
        user = userService.register(user);
        return R.success(user);
    }
}
